package peishui.activities;

import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xkh.xkh.R;

import com.xkh.xkh.MyApplication;

import managers.UIHandler;
import shuiyuqing.network.beans.IrrBAWaterGate;
import shuiyuqing.network.beans.RealTimeIrrBRWaterDam;
import shuiyuqing.network.http.GetRealWaterGateDam;
import utils.TimeUtil;

/**
 * 闸门信息
 * Created by 凯 on 2015/11/10.
 */
public class GateActivity extends UIHandler.BaseActivity {


    // 返回按钮
    Button btn_back;

    // 刷新显示界面
    View v_refresh;


    // 闸门名称
    TextView tv_gateName;

    // 图片
    ImageView img_img;

    // 采集时间
    TextView tv_time;

    // 闸上水位
    TextView tv_upLevel;

    // 闸下数拒
    TextView tv_downLevel;

    // 过闸流量
    TextView tv_flow;

    // 开闸孔数
    TextView tv_openDot;

    // 开度1
    TextView tv_open1;

    // 开度2
    TextView tv_open2;

    // 开度3
    TextView tv_open3;

    // 开度4
    TextView tv_open4;

    // 开度5
    TextView tv_open5;

    // 操作状态
    TextView tv_operStatus;

    // 界面显示修正
    View v_open1;
    View v_open2;
    View v_open3;
    View v_open4;
    View v_open5;


    // 手势
    GestureDetector mGestureDetector;

    IrrBAWaterGate gate;
    // 有可能为空
    RealTimeIrrBRWaterDam dam;

    public TextView getTv_gateName() {
        return tv_gateName;
    }

    public ImageView getImg_img() {
        return img_img;
    }

    public TextView getTv_time() {
        return tv_time;
    }

    public TextView getTv_upLevel() {
        return tv_upLevel;
    }

    public TextView getTv_downLevel() {
        return tv_downLevel;
    }

    public TextView getTv_flow() {
        return tv_flow;
    }

    public TextView getTv_openDot() {
        return tv_openDot;
    }

    public TextView getTv_open1() {
        return tv_open1;
    }

    public TextView getTv_open2() {
        return tv_open2;
    }

    public TextView getTv_open3() {
        return tv_open3;
    }

    public TextView getTv_open4() {
        return tv_open4;
    }

    public TextView getTv_open5() {
        return tv_open5;
    }

    public TextView getTv_operStatus() {
        return tv_operStatus;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gate);

        initdata();

        initview();
    }

    // 初始化数据
    private void initdata() {
        gate = (IrrBAWaterGate) getIntent().getSerializableExtra("gate");
        dam = (RealTimeIrrBRWaterDam) getIntent().getSerializableExtra("dam");
    }


    View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_back) {
                finish();
            }
        }
    };


    private void initview() {
        // 返回按钮
        btn_back = (Button) findViewById(R.id.id_btn_back);
        btn_back.setOnClickListener(itemClick);

        v_refresh = findViewById(R.id.id_v_refresh);

        tv_gateName = (TextView) findViewById(R.id.id_tv_gateName);

        img_img = (ImageView) findViewById(R.id.id_img_img);

        tv_time = (TextView) findViewById(R.id.id_tv_time);

        tv_upLevel = (TextView) findViewById(R.id.id_tv_upWaterLevel);

        tv_downLevel = (TextView) findViewById(R.id.id_tv_downWaterLevel);

        tv_flow = (TextView) findViewById(R.id.id_tv_flow);

        tv_open1 = (TextView) findViewById(R.id.id_tv_open1);
        tv_open2 = (TextView) findViewById(R.id.id_tv_open2);
        tv_open3 = (TextView) findViewById(R.id.id_tv_open3);
        tv_open4 = (TextView) findViewById(R.id.id_tv_open4);
        tv_open5 = (TextView) findViewById(R.id.id_tv_open5);

        v_open1 = findViewById(R.id.id_v_open1);
        v_open2 = findViewById(R.id.id_v_open2);
        v_open3 = findViewById(R.id.id_v_open3);
        v_open4 = findViewById(R.id.id_v_open4);
        v_open5 = findViewById(R.id.id_v_open5);

        tv_openDot = (TextView) findViewById(R.id.id_tv_openDot);
        tv_operStatus = (TextView) findViewById(R.id.id_tv_operStatus);
        initGateWith();
        initviewWithdataAndNetwork(gate, dam);
    }

    // 设计开闸门数目
    private void initGateWith() {
        if (gate != null) {
            if (gate.getWatergateNUM() != null) {
                int gateNum = gate.getWatergateNUM().intValue();

                switch (gateNum) {
                    case 0:
                        v_open1.setVisibility(View.GONE);
                        v_open2.setVisibility(View.GONE);
                        v_open3.setVisibility(View.GONE);
                        v_open4.setVisibility(View.GONE);
                        v_open5.setVisibility(View.GONE);
                        break;
                    case 1:
                        v_open1.setVisibility(View.VISIBLE);
                        v_open2.setVisibility(View.GONE);
                        v_open3.setVisibility(View.GONE);
                        v_open4.setVisibility(View.GONE);
                        v_open5.setVisibility(View.GONE);
                        break;
                    case 2:
                        v_open1.setVisibility(View.VISIBLE);
                        v_open2.setVisibility(View.VISIBLE);
                        v_open3.setVisibility(View.GONE);
                        v_open4.setVisibility(View.GONE);
                        v_open5.setVisibility(View.GONE);
                        break;
                    case 3:
                        v_open1.setVisibility(View.VISIBLE);
                        v_open2.setVisibility(View.VISIBLE);
                        v_open3.setVisibility(View.VISIBLE);
                        v_open4.setVisibility(View.GONE);
                        v_open5.setVisibility(View.GONE);
                        break;
                    case 4:
                        v_open1.setVisibility(View.VISIBLE);
                        v_open2.setVisibility(View.VISIBLE);
                        v_open3.setVisibility(View.VISIBLE);
                        v_open4.setVisibility(View.VISIBLE);
                        v_open5.setVisibility(View.GONE);
                        break;
                    case 5:
                        v_open1.setVisibility(View.VISIBLE);
                        v_open2.setVisibility(View.VISIBLE);
                        v_open3.setVisibility(View.VISIBLE);
                        v_open4.setVisibility(View.VISIBLE);
                        v_open5.setVisibility(View.VISIBLE);
                        break;
                }
            }
        }
    }

    // 刷新
    private void onRefresh() {
        v_refresh.setVisibility(View.VISIBLE);
        UIHandler.uiHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                v_refresh.setVisibility(View.GONE);
            }
        }, 1000);

        if (gate != null) {
            new GetRealWaterGateDam(gate.getStadiaCode()).get();
        }
    }


    // 设置数据并进行网络请求
    public void initviewWithdataAndNetwork(IrrBAWaterGate gate, RealTimeIrrBRWaterDam dam) {

        if (dam != null) {
            tv_time.setText(TimeUtil.timestamp2Format(dam.getMeaTime().getTimeInMillis()));
            tv_upLevel.setText(dam.getHeightUp() + "");
            tv_downLevel.setText(dam.getHeightDown() + "");
            tv_flow.setText(dam.getFluxGate() + "");
            tv_open1.setText(dam.getOpenDegree1() + "");
            tv_open2.setText(dam.getOpenDegree2() + "");
            tv_open3.setText(dam.getOpenDegree3() + "");
            tv_open4.setText(dam.getOpenDegree4() + "");
            tv_open5.setText(dam.getOpenDegree5() + "");
            tv_openDot.setText(dam.getGateOpenCount() + "");

            String str_status = dam.getGateState();
            if (str_status != null) {
                if (str_status.trim().equals("00")) {
                    tv_operStatus.setText("开闸门");
                } else if (str_status.trim().equals("01")) {
                    tv_operStatus.setText("关闸门");
                } else {
                    tv_operStatus.setText("无操作");
                }
            }
            if (gate != null) {
                tv_gateName.setText(gate.getWatergateName().trim());
//            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
//            tv_gateName.setLayoutParams(lp);
                new GetRealWaterGateDam(gate.getStadiaCode()).get();
            }


        }

    }


    // 设置数据
    public void initviewWithdata(IrrBAWaterGate gate, RealTimeIrrBRWaterDam dam) {
        if (gate != null) {
            tv_gateName.setText(gate.getWatergateName());
//            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            lp.addRule(RelativeLayout.CENTER_IN_PARENT);
//            tv_gateName.setLayoutParams(lp);
        }

        if (dam != null) {
            tv_time.setText(TimeUtil.timestamp2Format(dam.getMeaTime().getTimeInMillis()));
            tv_upLevel.setText(dam.getHeightUp() + "");
            tv_downLevel.setText(dam.getHeightDown() + "");
            tv_flow.setText(dam.getFluxGate() + "");
            tv_open1.setText(dam.getOpenDegree1() + "");
            tv_open2.setText(dam.getOpenDegree2() + "");
            tv_open3.setText(dam.getOpenDegree3() + "");
            tv_open4.setText(dam.getOpenDegree4() + "");
            tv_open5.setText(dam.getOpenDegree5() + "");
            tv_openDot.setText(dam.getGateOpenCount() + "");

            String str_status = dam.getGateState();
            if (str_status != null) {
                if (str_status.trim().equals("00")) {
                    tv_operStatus.setText("开闸门");
                } else if (str_status.trim().equals("01")) {
                    tv_operStatus.setText("关闸门");
                } else {
                    tv_operStatus.setText("无操作");
                }
            }

//            new GetRealWaterGateDam(gate.getStadiaCode()).get();

        }

    }
}
